﻿using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

public enum SoldierTransition
{
    NullTransition = 0, //空的
    SeeEnemy,           //看到敌人
    NoEnemy,            //丢失敌人
    CanAttack           //可以攻击
}

public enum SoldierStateID
{
    NullState,//空状态
    Idle, //站立
    Chase, //追击
    Attack //攻击
}

/// <summary>
/// 状态机
/// </summary>
public abstract class ISoldierState
{    
    /// <summary>
    /// 存储所以转换条件  转换条件 转换状态
    /// </summary>
    protected Dictionary<SoldierTransition, SoldierStateID> mMap = new Dictionary<SoldierTransition, SoldierStateID>();
    /// <summary>
    /// 自己的状态
    /// </summary>
    protected SoldierStateID mStateID;

    protected ICharacter mCharacter;
    protected SoldierFSMSystem mFsm;

    public ISoldierState(SoldierFSMSystem fsm,ICharacter character)
    {
        mFsm = fsm;
        mCharacter = character;
    }

    public SoldierStateID stateID {
        get { return mStateID; }
    }

    /// <summary>
    /// 添加状态
    /// </summary>
    /// <param name="trans"></param>
    /// <param name="id"></param>
    public void AddTransition(SoldierTransition trans, SoldierStateID id)
    {
        if (trans == SoldierTransition.NullTransition)
        {
            Debug.LogError("ISoldierStateError: SoldierTransition 不能为空");
            return;
        }
        if (id == SoldierStateID.NullState)
        {
            Debug.LogError("ISoldierStateError: SoldierStateID 不能为空");
            return;
        }
        if (mMap.ContainsKey(trans))
        {
            Debug.LogError("ISoldierStateError:SoldierState已经存在了");
            return;
        }
        mMap.Add(trans,id);
    }

    /// <summary>
    /// 删除某个状态
    /// </summary>
    /// <param name="trans"></param>
    public void DeleteTransition(SoldierTransition trans)
    {
        if (mMap.ContainsKey(trans) == false)
        {
            Debug.LogError("删除转换条件:["+ trans+"]不存在");
            return;
        }
        mMap.Remove(trans);
    }

    /// <summary>
    /// 获得某个转换条件的状态 类型
    /// </summary>
    /// <param name="trans"></param>
    /// <returns></returns>
    public SoldierStateID GetOutPutState(SoldierTransition trans)
    {
        if (mMap.ContainsKey(trans) == false)
        {

            return SoldierStateID.NullState;
        }
        else
        {
            return mMap[trans];
        }
    }

# region //有的状态可能要初始化可以选择性重写
     
    public virtual void DoBeforeEntering() { }

    public virtual void DoBeforeLeaving() {  }
    #endregion
  
    /// <summary>
    /// 判断是否需要转换到别的状态
    /// </summary>
    /// <param name="targets">目标集合</param>
    public abstract void Reason(List<ICharacter> targets);
    /// <summary>
    /// 处理当前状态的游戏逻辑
    /// </summary>
    public abstract void Act(List<ICharacter> targets);
}

